package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.mapper.*;
import com.example.demo.aop.Idempotents;
import com.example.demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Controller
public class AddScoreController {
    @Autowired
    private ScoreMapper sm;
    @Autowired
    private StudentMapper stm;
    @Autowired
    private CourseMapper cm;
    @Autowired
    private ClassiMapper cim;
    @Autowired
    private CourseNumMapper cnm;

    private ConcurrentHashMap<Integer, String> termsMap = new ConcurrentHashMap<>();

    @RequestMapping("/addscore")
    @Idempotents
    public void addScore(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int flag=sm.selectLast().getFlag();
        int id=Integer.parseInt(request.getParameter("id"));
        String name=request.getParameter("name");
        String major=request.getParameter("major");
        String classi=request.getParameter("classi");
        String course=request.getParameter("course");
        String teachername=request.getParameter("teachername");
        Float gredit=Float.valueOf(request.getParameter("gredit"));
        Float escore=Float.valueOf(request.getParameter("escore"));
        Float ascore=Float.valueOf(request.getParameter("ascore"));
        int fscore=Integer.parseInt(request.getParameter("fscore"));
        Student s=stm.selectStuById(id);
        if(s.getName().equals(name)&&s.getMajor().equals(major)&&s.getClassi().equals(classi)){
            int tid=cim.findAluClassi(id,course).getTid();
            Course cou=cm.selectCourseByIds(tid);
            if(cou.getTeachername().equals(teachername)&&cou.getCoursename().equals(course)&& Objects.equals(cou.getGredit(), gredit)){
                int ffscore=(int)Math.round(escore*0.6+ascore*0.4);
                if(ffscore==fscore){
                    List<CourseNum> courseNums = cnm.selectCourseNumById(id);
                    Comparator<CourseNum> comparator = new Comparator<CourseNum>() {
                        @Override
                        public int compare(CourseNum o1, CourseNum o2) {
                            String[] split = o1.getTerm().split("-");
                            String[] split1 = o2.getTerm().split("-");
                            int grade=Integer.valueOf(split[0]);
                            int grade1=Integer.valueOf(split1[0]);
                            int term =Integer.valueOf(split[2]);
                            int term1 =Integer.valueOf(split1[2]);
                            String terms = "";
                            if(grade>grade1) {
                                terms=grade+"-"+grade+1+"-"+term+"";
                            }else if(grade<grade1) {
                                terms=grade1+"-"+grade1+1+"-"+term1+"";
                            }else {
                                if(term>term1) terms=grade1+"-"+grade1+1+"-"+term+"";
                                else if(term<term1) terms=grade1+"-"+grade1+1+"-"+term1+"";
                            }
                            termsMap.put(id, terms);
                            return 0;
                        }
                    };
                    Collections.max(courseNums, comparator);
                    Score ss=new Score(flag+1,id,name,major,classi,course,teachername,gredit,escore,ascore,fscore,termsMap.get(id));
                    termsMap.remove(id, termsMap.get(id));
                    int n=sm.addScore(ss);
                    if(n>0){
                        response.getWriter().write(JSONObject.toJSONString(Message.success("Success add!")));
                    }else response.getWriter().write(JSONObject.toJSONString(Message.failure("Failure add!")));
                }else response.getWriter().write(JSONObject.toJSONString(Message.failure("You input final score is error!")));
            }else response.getWriter().write(JSONObject.toJSONString(Message.failure("You input teacherinfo or gredit is error!")));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("You input studentinfo is error!")));
    }
}
